package com.easy.modules.sysmgr.web.action;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easy.common.util.Const;
import com.easy.common.util.file.FileUtils;
import com.easy.common.web.page.DataGrid;
import com.easy.common.web.page.Msg;
import com.easy.modules.sysmgr.service.DataBaseServiceI;
import com.easy.modules.sysmgr.web.form.BackupDBForm;


@Controller
@RequestMapping("/sysmgr/dbmgr")
public class DataBaseAction {
	
	@Autowired
    private DataBaseServiceI dbservice ;

	@RequestMapping("/database_main.do")
	public String database_main(Model mode) {
		mode.addAttribute("databaseName", this.dbservice.getDefaultCatalog()) ;
		return Const.SYSMGR + "database_main" ;
	}
	
	@RequestMapping("/doNotNeedAuth_table_datail.do")
	public String doNotNeedAuth_table_datail(BackupDBForm form, Model mode) {
		mode.addAttribute("databaseName", form.getDatabaseName()) ;
		mode.addAttribute("table", form.getTable()) ;
		return Const.SYSMGR + "database_table_datail" ;
	}
	
	@RequestMapping("/doNotNeedAuth_table_sql.do")
	public String doNotNeedAuth_table_info(BackupDBForm form, Model mode) {
		mode.addAttribute("databaseName", form.getDatabaseName()) ;
		mode.addAttribute("table", form.getTable()) ;
		return Const.SYSMGR + "database_table_sql" ;
	}
	
	@RequestMapping("/doNotNeedAuth_databases.do")
	@ResponseBody
	public List<Map<String, Object>> doNotNeedAuth_databases() {
		return this.dbservice.showDataBases() ;
	}
	
	@RequestMapping("/doNotNeedAuth_tables.do")
	@ResponseBody
	public List<Map<String, Object>> doNotNeedAuth_tables() {
		return this.dbservice.showDataBases() ;
	}
	
	@RequestMapping("/doNotNeedAuth_tableAttr.do")
	@ResponseBody
	public List<Map<String, Object>> doNotNeedAuth_tableAttr(BackupDBForm form) {
		return this.dbservice.tableAttr(form) ;
	}
	
	@RequestMapping("/doNotNeedAuth_tableScript.do")
	@ResponseBody
	public Map<String, Object> doNotNeedAuth_tableScript(BackupDBForm form) {
		return this.dbservice.tableScript(form) ;
	}

	@RequestMapping("/doNotNeedAuth_tablesInformation.do")
	@ResponseBody
	public List<Map<String, Object>> doNotNeedAuth_tablesInformation(BackupDBForm form) {
		return this.dbservice.tablesInformation(form) ;
	}
	
	@RequestMapping("/backupDatabase.do")
	@ResponseBody
	public Msg backupDatabase(BackupDBForm form) {
		return this.dbservice.backupDataBase(form) ;
	}
	
	@RequestMapping("/backupTables.do")
	@ResponseBody
	public Msg backupTables(BackupDBForm form) {
		return this.dbservice.backupDataBase(form) ;
	}
	
	@RequestMapping("/recoverDatabase.do")
	@ResponseBody
	public Msg recoverDatabase(BackupDBForm form) {
		return this.dbservice.recoverDatabase(form) ;
	}
	
	@RequestMapping("/recoverTables.do")
	@ResponseBody
	public Msg recoverTables(BackupDBForm form) {
		return this.dbservice.recoverTables(form) ;
	}
	
	@RequestMapping("/doNotNeedAuth_datagrid.do")
	@ResponseBody
	public DataGrid doNotNeedAuth_datagrid(BackupDBForm form) {
		return this.dbservice.datagrid(form) ;
	}
	
	@RequestMapping("/deleteBackup.do")
	@ResponseBody
	public Msg deleteBackup(BackupDBForm form) {
		return this.dbservice.deleteBackup(form) ;
	}
	
	@RequestMapping("/download.do")
	public void deleteBackup(BackupDBForm form, HttpServletRequest request, HttpServletResponse response) {
		try {
			String filename = URLDecoder.decode(form.getDatabaseFullName(),"utf-8") ;
			String srcDirName = new File(FileUtils.getAbsolutePath()).getParent() + File.separator + form.getFolder() ;
			FileUtils.downloadFile(srcDirName, filename, request, response) ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}
